//引入核心模块
const http = require("http");
const https = require("https");
const fs = require("fs");
const cheerio = require("cheerio");
let wait = ms => new Promise(resolve => setTimeout(resolve, ms));
init();
async function init() {
    //定义一个延时方法
    for (let page = 114; page < 160; page++) {
        await wait(10000);
        let url = `https://pic.netbian.com/4kdongman/index_${page}.html`
        // let indexurl = `https://pic.netbian.com/4kdongman/`
        //创建服务
        https.get(url, {
            proxy: {
                host: '127.0.0.1',
                port: 3002,
                auth: {}
            }
        }, (res) => {
            //触发接收事件data
            let imageData = "";
            res.on("data", (buffer) => {
                //接收数据，将数据一点点的追加到没我们定义的空字符串中
                imageData += buffer;
            });
            //触发接收完成时间end
            res.on("end", () => {
                //cheerio类似于jQuery我们在用他之前必须载入文档用（load方法）
                const $ = cheerio.load(imageData);
                let urlDomList = [];
                //遍历查出来的文本数据
                $(".slist .clearfix li a").each((index, item) => {
                    //定义一个空数组，将数据内容存在里面 
                    // let imgUrl = `https://pic.netbian.com/` 
                    let imgUrl = `https://pic.netbian.com/` + $(item).attr("href");
                    urlDomList.push(imgUrl);
                });
                getImg(urlDomList, page)
            });
        });
    }
}


// 循环获取的DOM URl 获取图片URL并存储
function getImg(params, pagesNum) {
    params.forEach(domUrl => {
        getImgUrlList(domUrl).then(res => {
            saveImage(res[0], pagesNum)
        });
    })
}

// 根据传入的DOMURL网址获取图片IMGURL
function getImgUrlList(domUrl) {
    // console.log('imgDom', domUrl);
    return new Promise((resolve, reject) => {
        let imgList = [];
        let imageStr = '';
        //创建服务
        https.get(domUrl, (res) => {
            res.on("data", (buffer) => {
                imageStr += buffer;
            });
            res.on('end', () => {
                const $ = cheerio.load(imageStr);
                $(".photo-pic #img img").each((index, item) => {
                    let imgUrl = `https://pic.netbian.com/` + $(item).attr("src");
                    imgList.push(imgUrl);
                });
                resolve(imgList);
            })
        });
    })
}

//封装图片函数
function saveImage(imageUrl, pagesNum) {
    //创建服务
    https.get(imageUrl, (res) => {
        //二进制
        res.setEncoding("binary");
        let imageData = "";
        //将图片加载到内存中
        res.on("data", (shuju) => {
            imageData += shuju;
        });

        //加载完保存图片
        res.on("end", () => {
            // //创建文件夹，如果有择不创建
            if (!fs.existsSync(`./myFirstReptile/img${pagesNum}`)) {
                fs.mkdirSync(`./myFirstReptile/img${pagesNum}`);
            };
            //保存图片
            fs.writeFile(`./myFirstReptile/img${pagesNum}/` + parseInt(Math.random() * 100000000) + '.png', imageData, "binary", (err) => {
                if (!err) console.log(`第${pagesNum}页保存成功！！！`)
            })
        })
    })
}
